class IndexedDBCache {
    constructor(storeName, keyPath) {
        this.dbName = 'cache'
        this.storeName = storeName
        this.version = 1
        this.db = null
        this.request = null
        this.keyPath = keyPath
    }

    init() {
        return new Promise((resolve, reject) => {
            this.request = window.indexedDB.open(this.dbName, this.version)
            this.request.onerror = (event) => {
                reject({
                    code: 500,
                    message: '数据库初始化失败'
                })
            }
            this.request.onsuccess = (event) => {
                this.db = event.target.result
                resolve({
                    code: 200,
                    data: {
                        name: this.db.name,
                        version: this.db.version,
                        keyPath: this.keyPath
                    }
                })
            }
            this.request.onupgradeneeded = (event) => {
                this.db = event.target.result
                if (!this.db.objectStoreNames.contains(this.storeName)) {
                    this.db.createObjectStore(this.storeName, { keyPath: this.keyPath })
                }
            }
        })
    }

    get(key) {
        return new Promise((resolve, reject) => {
            const transaction = this.db.transaction([this.storeName], 'readonly')
            const objectStore = transaction.objectStore(this.storeName)
            const request = objectStore.get(key)
            request.onerror = (event) => {
                reject(event)
            }
            request.onsuccess = (event) => {
                resolve(event.target.result)
            }
        })
    }

    // 批量添加或更新数据
    insert(value) {
        return new Promise((resolve, reject) => {
            const transaction = this.db.transaction([this.storeName], 'readwrite')
            const objectStore = transaction.objectStore(this.storeName)
            const request = objectStore.add(value)
            request.onerror = (event) => {
                reject(event)
            }
            request.onsuccess = (event) => {
                resolve(event)
            }
        })
    }

    // 添加或更新数据
    update(key, value) {
        return new Promise((resolve, reject) => {
            const transaction = this.db.transaction([this.storeName], 'readwrite')
            const objectStore = transaction.objectStore(this.storeName)
            const request = objectStore.put(value, key)
            request.onerror = (event) => {
                reject(event)
            }
            request.onsuccess = (event) => {
                resolve(event)
            }
        })
    }
    // 删除数据
    remove(key) {
        return new Promise((resolve, reject) => {
            const transaction = this.db.transaction([this.storeName], 'readwrite')
            const objectStore = transaction.objectStore(this.storeName)
            const request = objectStore.delete(key)
            request.onerror = (event) => {
                reject(event)
            }
            request.onsuccess = (event) => {
                resolve(event)
            }
        })
    }
    // 清空数据
    clear() {
        return new Promise((resolve, reject) => {
            const transaction = this.db.transaction([this.storeName], 'readwrite')
            const objectStore = transaction.objectStore(this.storeName)
            const request = objectStore.clear()
            request.onerror = (event) => {
                reject(event)
            }
            request.onsuccess = (event) => {
                resolve(event)
            }
        })
    }
    // 关闭数据库
    close() {
        this.db.close()
    }
    // 销毁数据库
    destroy() {
        return new Promise((resolve, reject) => {
            const request = window.indexedDB.deleteDatabase(this.dbName)
            request.onerror = (event) => {
                reject(event)
            }
            request.onsuccess = (event) => {
                resolve(event)
            }
        })
    }
}

export default IndexedDBCache